package com.sicau.smart_farmer.controller;

import com.sicau.smart_farmer.common.CommonConstants;
import com.sicau.smart_farmer.common.ResultVO;
import com.sicau.smart_farmer.entity.User;
import com.sicau.smart_farmer.service.UserService;
import com.sicau.smart_farmer.service.UserTrainingRecordService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "用户")
@Slf4j
@Controller
@RequestMapping(CommonConstants.USER_PREFIX)
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserTrainingRecordService userTrainingRecordService;

    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "user", required = true, value = "用户的所有基本信息")
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO addUser(@RequestBody User user) {
        boolean res = userService.addUser(user);
        if (res) {
            return ResultVO.FAILED();
        }

        return ResultVO.SUCCESS();

    }

    @ApiOperation("根据名字或者身份证号模糊查询")
    @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "用户名"),
            @ApiImplicitParam(name = "idCardNum",  value = "身份证号") })
    @GetMapping(value = "searchUserLike")
    @ResponseBody
    public ResultVO searchUserLike(String name, String idCardNum) {
        if ( (name==null||name.equals("")) && (idCardNum==null||idCardNum.equals("")) ){
            return ResultVO.FAILED("用户名和身份证不能同时为空。");
        }
        List<User> users = userService.searchUserLike(name, idCardNum);
        // model.addAttribute("users",users);
        return ResultVO.SUCCESS(users).setCount(users.size());
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams({ @ApiImplicitParam(name = "userIdList", required = true, value = "用户id数组") })
    @GetMapping(value = "deleteUser")
    @ResponseBody
    public ResultVO deleteUser(@RequestParam("userIdList") List<String> userIdList) {
        for (String userId : userIdList) {
            // 先删除user集合里面的数据
            userService.deleteUserById(userId);
            // 再删除userTrainingRecord集合里面的数据
            userTrainingRecordService.deleteUserTrainingRecordByUserId(userId);
        }
        return ResultVO.SUCCESS();
    }

    @ApiOperation("更新用户")
    @ApiImplicitParams({ @ApiImplicitParam(name = "User", required = true, value = "被修改后用户的基本信息，必须所有信息全部重新上传") })
    @PostMapping(value = "updateUser")
    @ResponseBody
    public ResultVO updateUser(@RequestBody User user) {
        User updateUser = userService.updateUser(user);
        if (updateUser == null) {
            return ResultVO.FAILED("请检查用户id是否正确");
        }
        return ResultVO.SUCCESS(updateUser);
    }

    @ApiOperation("分页查找用户")
    @ApiImplicitParams({ @ApiImplicitParam(name = "page", required = true, value = "页码"),
            @ApiImplicitParam(name = "limit", required = true, value = "每页显示的条数"),

    })
    @GetMapping("selectAllUserByPage")
    @ResponseBody
    public ResultVO selectAllUserByPage(@RequestParam("page") Integer page, @RequestParam("limit") Integer pageNum, @ApiIgnore HttpSession session) {
        List<User> users = userService.selectAllUserByPage(page, pageNum);
        for(User u : users){//将查询数据保存到session
            session.setAttribute(u.getId(),u);
        }
        List<User> allUser = userService.selectAllUser();
        return ResultVO.SUCCESS(users).setCount(allUser.size());//分页

    }

    @ApiOperation("根据用户id查询用户")
    @ApiImplicitParams({ @ApiImplicitParam(name = "userId", required = true, value = "用户id") })
    @GetMapping("/selectUserById")
    @ResponseBody
    public ResultVO selectUserById(String userId) {
        User user = userService.selectUserById(userId);
        return ResultVO.SUCCESS(user);
    }

    @ApiOperation("批量更新用户的新型职业农民状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIdList",required = true,value = "userId的列表"),
            @ApiImplicitParam(name = "identification",required = true,value = "新型职业农民状态",dataType = "boolean")
    })
    @PostMapping("/updateIdentification")
    @ResponseBody
    public ResultVO updateIdentification(@RequestParam("userIdList")List<String> userIdList,boolean identification) {
        System.out.println(userIdList);
        System.out.println(userIdList.size());
        System.out.println(identification);
        userService.updateUserIdentification(userIdList, identification);
        return ResultVO.SUCCESS();
    }

    @ApiOperation(value = "获取所有（非）新型农民")
    @GetMapping(value = "getAllIdentifiedUser")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isIdentified",required = true,dataType = "boolean",value = "true：查询所有新型农民  false:查询所有非新型农民"),
    })
    @ResponseBody
    public ResultVO getAllIdentifiedUser(boolean isIdentified){

        List<User> list = userService.queryAllIdentifiedUser(isIdentified);

        return ResultVO.SUCCESS(list);

    }

}
